<?php
if (version_compare(phpversion(), '5', '<') === true)
{
	exit('phpFox 2.x requires PHP 5 or newer.');
}

define('PHPFOX', true);
define('PHPFOX_DS', DIRECTORY_SEPARATOR);
define('PHPFOX_DIR', dirname(__FILE__) . PHPFOX_DS);
require (PHPFOX_DIR . 'include' . PHPFOX_DS . 'init.inc.php');
require (PHPFOX_DIR . 'include' . PHPFOX_DS . 'setting' . PHPFOX_DS . 'server.sett.php');
define('ADMIN_GROUP', 1);
define('PHPFIX',$_CONF['db']['prefix']);

$user = new userdata();
$sUserName = $_GET['username'];
$sDestUserName = $_GET['destusername'];
if( empty($sUserName) )
{
   exit;
}

$db = new Phpfox_Database_Driver_Mysql;
if(!$db -> connect($_CONF['db']['host'], $_CONF['db']['user'], $_CONF['db']['pass'],$_CONF['db']['name']))
{
   exit;
}

$sUserName = $db ->escape($sUserName);
$sDestUserName = $db ->escape($sDestUserName);

//query id
	//user_id
$sql = 'SELECT * from '.$_CONF['db']['prefix'].'user WHERE user_name=\''.$sUserName.'\'';
$getdb = $db -> getRows ($sql);
$fetch = $getdb[0];
$user_id = $fetch['user_id'];
	//dest_id
$sql_dest = 'SELECT * from '.$_CONF['db']['prefix'].'user WHERE user_name=\''.$sDestUserName.'\'';
$getdb_dest = $db -> getRows ($sql_dest);
$fetch_dest = $getdb_dest[0];
$dest_id = $fetch_dest['user_id'];

//queryFriend
$queryFriendSql = 'SELECT friend_user_id from '.$_CONF['db']['prefix'].'friend WHERE user_id=\''.$user_id.'\'';
	//$getdb_dest = mysql_query($queryFriendSql);
	//while($getdb_des = mysql_fetch_array($getdb_dest)){
	//var_dump($getdb_des['friend_user_id']);}exit;
$opQueryFriendSql = 'SELECT user_id from '.$_CONF['db']['prefix'].'friend_request WHERE friend_user_id=\''.$user_id.'\'';
//addFriend
	//kan ta shi fou yi jing shi wo de  hao you
$addFriendSqlOneo = 'SELECT * from '.$_CONF['db']['prefix'].'friend WHERE user_id=\''.$user_id.'\' and friend_user_id=\''.$dest_id.'\'';
	//kanta shi fou ta yijing shengqing wo wei ta de haoyou
$addFriendSqlOne = 'SELECT friend_user_id from '.$_CONF['db']['prefix'].'friend_request WHERE user_id=\''.$user_id.'\' and friend_user_id=\''.$dest_id.'\'';
		//ru guo ta yi jing shengqing jia le wo
$addFriendSqlTwo = 'delete from '.$_CONF['db']['prefix'].'friend_request WHERE user_id=\''.$user_id.'\' and friend_user_id=\''.$dest_id.'\'';
				//friend_list cha xun shi fou friend list biao zhogn you zhege list one
$addFriendSqlThreeo = 'SELECT * from '.$_CONF['db']['prefix'].'friend_list WHERE user_id=\''.$user_id.'\'';
				//friend_list cha xun shi fou friend list biao zhogn you zhege list two
$addFriendSqlThreet = 'SELECT * from '.$_CONF['db']['prefix'].'friend_list WHERE user_id=\''.$dest_id.'\'';
				//friend_list ruguo meiyou user_id de hua time mei jia
$addFriendSqlFouro = 'insert into '.$_CONF['db']['prefix'].'friend_list (`user_id`,`name`,`used`) values(\''.$user_id.'\',\''.$sUserName.'\',\'0\')';
				//friend_list ruguo mei you dest_id de hua
$addFriendSqlFourt = 'insert into '.$_CONF['db']['prefix'].'friend_list (`user_id`,`name`,`used`) values(\''.$dest_id.'\',\''.$sDestUserName.'\',\'0\')';
				//friend  de biao de cao zuo  time mei jia
$addFriendSqlFiveo = 'insert into '.$_CONF['db']['prefix'].'friend (`list_id`,`user_id`,`friend_user_id`,`is_top_friend`,`ordering`,`time_stamp`) values (\''.$user_id.'\',\''.$user_id.'\',\''.$dest_id.'\',\'0\',\'0\',\'\');';
$addFriendSqlFivet = 'insert into '.$_CONF['db']['prefix'].'friend (`list_id`,`user_id`,`friend_user_id`,`is_top_friend`,`ordering`,`time_stamp`) values (\''.$dest_id.'\',\''.$dest_id.'\',\''.$user_id.'\',\'0\',\'0\',\'\');';
		//ru guo ta mei jia wo de  hua
$addFriendSqlSix = 	'insert into '.$_CONF['db']['prefix'].'friend_request (`user_id`,`friend_user_id`,`is_ignore`,`list_id`,`message`,`time_stamp`) values (\''.$dest_id.'\',\''.$user_id.'\',\'0\',\''.$user_id.'\',\'\',\'\');';
$addFriendSqlSixt = 'SELECT * from '.$_CONF['db']['prefix'].'friend_request WHERE friend_user_id=\''.$user_id.'\' and user_id=\''.$dest_id.'\'';

//removeFriend
	//pan duan ta shi fou yi jing shi  wo wei ta de haoyou
$removeFriendSqlOneo = 'SELECT * from '.$_CONF['db']['prefix'].'friend WHERE user_id=\''.$user_id.'\' and friend_user_id=\''.$dest_id.'\'';
		//ru guo wo de friend biao zhong meiyou de hua ,na me  cha xun ta shi fou zhengzai shengqing wo wei ta de haoyou
$removeFriendSqlOnet = 'SELECT * from '.$_CONF['db']['prefix'].'friend_request WHERE user_id=\''.$user_id.'\' and friend_user_id=\''.$dest_id.'\'';
		// ta sheng qing wo wei haoyou  ran hou wo ju jue ta
$removeFriendSqlOne = 'delete from '.$_CONF['db']['prefix'].'friend_request WHERE user_id=\''.$user_id.'\' and friend_user_id=\''.$dest_id.'\'';
		// ta yijiang shi wo de haoyou le  ran hou wo remove ta
				//friend biao
$removeFriendSqlTwo = 'delete from '.$_CONF['db']['prefix'].'friend WHERE user_id=\''.$user_id.'\' and friend_user_id=\''.$dest_id.'\'';
$removeFriendSqlThree = 'delete from '.$_CONF['db']['prefix'].'friend WHERE user_id=\''.$dest_id.'\' and friend_user_id=\''.$user_id.'\'';
				//friend_list biao
						//cha xun friend shi fou haiyou zhege user_id
$removeFriendSqlFour = 'SELECT * from '.$_CONF['db']['prefix'].'friend WHERE user_id=\''.$user_id.'\'';
						//cha xun friend shi fou haiyou zhege user_id xin jia de
$removeFriendSqlFourt = 'SELECT * from '.$_CONF['db']['prefix'].'friend WHERE friend_user_id=\''.$user_id.'\'';
							//ru guo mei you zhege userid de hua
$removeFriendSqlFive =	'delete from '.$_CONF['db']['prefix'].'friend_list WHERE user_id=\''.$user_id.'\'';
						//cha xun friend shi fou haiyou zhege destid
$removeFriendSqlSix = 'SELECT * from '.$_CONF['db']['prefix'].'friend WHERE user_id=\''.$dest_id.'\'';
						////cha xun friend shi fou haiyou zhege destid  xin jia de
$removeFriendSqlSixt = 'SELECT * from '.$_CONF['db']['prefix'].'friend WHERE friend_user_id=\''.$dest_id.'\'';
							//ru guo meiyou de hua destid
$removeFriendSqlSeven =	'delete from '.$_CONF['db']['prefix'].'friend_list WHERE user_id=\''.$dest_id.'\'';






$userNameRowField = "user_name";

//query sql
$user->queryFriendSql($queryFriendSql);
$user->opQueryFriendSql($opQueryFriendSql);

//add sql
$user->addFriendSqlOneo($addFriendSqlOneo);
$user->addFriendSqlOne($addFriendSqlOne);
$user->addFriendSqlTwo($addFriendSqlTwo);
$user->addFriendSqlThreeo($addFriendSqlThreeo);
$user->addFriendSqlThreet($addFriendSqlThreet);
$user->addFriendSqlFouro($addFriendSqlFouro);
$user->addFriendSqlFourt($addFriendSqlFourt);
$user->addFriendSqlFiveo($addFriendSqlFiveo);
$user->addFriendSqlFivet($addFriendSqlFivet);
$user->addFriendSqlSix($addFriendSqlSix);
$user->addFriendSqlSixt($addFriendSqlSixt);

//remove
$user->removeFriendSqlOneo = $removeFriendSqlOneo;
$user->removeFriendSqlOnet = $removeFriendSqlOnet;
$user->removeFriendSqlOne = $removeFriendSqlOne;
$user->removeFriendSqlTwo = $removeFriendSqlTwo;
$user->removeFriendSqlThree = $removeFriendSqlThree;
$user->removeFriendSqlFour = $removeFriendSqlFour;
$user->removeFriendSqlFourt = $removeFriendSqlFourt;
$user->removeFriendSqlFive = $removeFriendSqlFive;
$user->removeFriendSqlSix = $removeFriendSqlSix;
$user->removeFriendSqlSixt = $removeFriendSqlSixt;
$user->removeFriendSqlSeven = $removeFriendSqlSeven;

// a var
$user->setUserNameRowField($userNameRowField);
$user->start();
class userdata
{
	private static $ACTION_QUERY = 0;
	private static $ACTION_ADD = 1;
	private static $ACTION_DELETE = 2;
	private static $ACTION_UPDATE = 3;


	private static $STRANGER = "stranger";
	private static $GENERAL = "general";

	private $removeFriendSql = "";
	private $queryFriendSql = "";
	private $db = "";

	public function userdata()
	{
		$this->setXmlDoc();
		$this->action  = $this->setAction();
		//stranger  general
		$this->friendGroup = $this->setFriendGroup();
	}

	public function start()
	{
		switch($this->action)
		{
			case self::$ACTION_ADD:
				$this->addFriends();
				break;
			case self::$ACTION_DELETE:
				$this->removeFriends();
				break;
			case self::$ACTION_QUERY:
				$this->getFriendList($this->queryFriendSql,self::$GENERAL);
				$this->getFriendListTwo($this->opQueryFriendSql,self::$STRANGER,true);
				$this->doc->appendChild($this->UD);
				break;
			case self::$ACTION_UPDATE:
				if($this->friendGroup == self::$STRANGER)
				{
					$this->removeFriends();
				}
				else if($this->friendGroup == self::$GENERAL)
				{
					$this->addFriends();
				}
		}
		$this->outputXml();
	}

//add
	private function addFriends()
	{
		if( $this->friendGroup == self::$STRANGER)//??
		{
				$this->responseSuccess();
		}
		else if($this->friendGroup == self::$GENERAL)
		{
			$addFriendSqlOneo = mysql_query($this->addFriendSqlOneo);
			if(mysql_num_rows($addFriendSqlOneo))
			{
				$this->responseSuccess();
			}
			else
			{
				$addFriendOne = mysql_query($this->addFriendSqlOne);
				if(mysql_num_rows($addFriendOne) > 0)
				{
					mysql_query($this->addFriendSqlTwo);
					$addFriendThreeo = mysql_query($this->addFriendSqlThreeo);
					if(mysql_num_rows($addFriendThreeo) > 0)
					{
						;
					}
					else
					{
						$fouro_query = mysql_query($this->addFriendSqlFouro);
						if(mysql_num_rows($fouro_query))
						{
							;
						}
						else
						{
							$this->responseFailed();
						}
					}

					$addFriendThreet = mysql_query($this->addFriendSqlThreet);
					if(mysql_num_rows($addFriendThreet) > 0)
					{
						;
					}
					else
					{
						$fourt_query = mysql_query($this->addFriendSqlFourt);
						if(mysql_num_rows($fourt_query))
						{
							;
						}
						else
						{
							$this->responseFailed();
						}
					}

					$fiveo_query = mysql_query($this->addFriendSqlFiveo);
					$fivet_query = mysql_query($this->addFriendSqlFivet);

					if(($fivet_query) && ($fiveo_query))
					{
						$this->responseSuccess();
					}
					else
					{
						$this->responseFailed();
					}
				}
				else
				{
					$sixt_query = mysql_query($this->addFriendSqlSixt);
					if(mysql_num_rows($sixt_query))
					{
						$this->responseSuccess();
					}
					else
					{
						$six_query = mysql_query($this->addFriendSqlSix);
						if($six_query)
						{
							$this->responseSuccess();
						}
						else
						{
							$this->responseFailed();
						}
					}
				}
			}

		}
		else
		{
			$this->responseFailed();
		}
	}


// remove
	private function removeFriends()
	{
		$removeOneo = mysql_query($this->removeFriendSqlOneo);
		if(mysql_num_rows($removeOneo) > 0)
		{
			$removeTwo = mysql_query($this->removeFriendSqlTwo);
			$removeThree = mysql_query($this->removeFriendSqlThree);
			if($removeTwo && $removeThree)
			{
				;
			}
			else
			{echo 123;
				$this->responseFailed();
			}

			$removeFour = mysql_query($this->removeFriendSqlFour);
			$removeFourt = mysql_query($this->removeFriendSqlFourt);

			if(mysql_num_rows($removeFour) || mysql_num_rows($removeFourt))
			{
				;
			}
			else
			{
				$removeFive = mysql_query($this->removeFriendSqlFive);
				if(mysql_num_rows($removeFive))
				{
					;
				}
				else
				{echo 456;
					$this->responseFailed();
				}
			}
			$removeSix = mysql_query($this->removeFriendSqlSix);
			$removeSixt = mysql_query($this->removeFriendSqlSixt);
			if(mysql_num_rows($removeSix) || mysql_num_rows($removeSixt))
			{
				;
			}
			else
			{
				$remvoeSeven = mysql_query($this->removeFriendSqlSeven);
				if($remvoeSeven)
				{
					;
				}
				else
				{echo 789;
					$this->responseFailed();
				}
			}
		}
		else
		{
			$removeOnet = mysql_query($this->removeFriendSqlOnet);
			if(mysql_num_rows($removeOnet))
			{
				$removeOne = mysql_query($this->removeFriendSqlOne);
				if(mysql_num_rows($removeOne))
				{
					$this->responseSuccess();
				}
				else
				{
					$this->responseFailed();
				}
			}
			else
			{
				$this->responseSuccess();
			}

		}
	}

//query
	private function getFriendList($sql,$type,$checkRow = false)
	{
		$rs = mysql_query($sql);
		$d = false;
		if(mysql_num_rows($rs) > 0)
		{
			$general = $this->doc->createElement("g");
			$general->setAttribute("n", $type);
			while($rss = mysql_fetch_array($rs)){
				$addrTwo = 'SELECT user_name from '.PHPFIX.'user WHERE user_id = \''.$rss['friend_user_id'].'\'';
				$rowo = mysql_query($addrTwo);
				$row = mysql_fetch_array($rowo);
				if(!$checkRow)
				{
					$d = true;
					$u = $this->doc->createElement("u");
					$u->setAttribute("n", $row[$this->userNameRowField]);
					$general->appendChild($u);
				}
			}
			if(!$checkRow || $d)$this->FL->appendChild($general);
			$this->UD->appendChild($this->FL);
		}
	}

	private function getFriendListTwo($sql,$type,$checkRow = false)
	{
		$rs = mysql_query($sql);
		$d = false;
		if(mysql_num_rows($rs) > 0)
		{
			$general = $this->doc->createElement("g");
			$general->setAttribute("n", $type);
			while($rss = mysql_fetch_array($rs)){
				$addrTwo = 'SELECT user_name from '.PHPFIX.'user WHERE user_id = \''.$rss['user_id'].'\'';
				$row = mysql_query($addrTwo);
				$roww = mysql_fetch_array($row);
				if(!$checkRow)
				{
					$d = true;
					$u = $this->doc->createElement("u");
					$u->setAttribute("n", $roww[$this->userNameRowField]);
					$general->appendChild($u);
				}
			}
			if(!$checkRow || $d)$this->FL->appendChild($general);
			$this->UD->appendChild($this->FL);
		}
	}

//query sql
	public function queryFriendSql($sql)
	{
		$this->queryFriendSql = $sql;
	}

	public function opQueryFriendSql($sql)
	{
		$this->opQueryFriendSql = $sql;
	}

//add sql

	public function addFriendSqlOneo($sql)
	{
		$this->addFriendSqlOneo = $sql;
	}

	public function addFriendSqlOne($sql)
	{
		$this->addFriendSqlOne = $sql;
	}

	public function addFriendSqlTwo($sql)
	{
		$this->addFriendSqlTwo = $sql;
	}

	public function addFriendSqlThreeo($sql)
	{
		$this->addFriendSqlThreeo = $sql;
	}

	public function addFriendSqlThreet($sql)
	{
		$this->addFriendSqlThreet = $sql;
	}

	public function addFriendSqlFouro($sql)
	{
		$this->addFriendSqlFouro = $sql;
	}

	public function addFriendSqlFourt($sql)
	{
		$this->addFriendSqlFourt = $sql;
	}

	public function addFriendSqlFiveo($sql)
	{
		$this->addFriendSqlFiveo = $sql;
	}

	public function addFriendSqlFivet($sql)
	{
		$this->addFriendSqlFivet = $sql;
	}

	public function addFriendSqlSix($sql)
	{
		$this->addFriendSqlSix = $sql;
	}

	public function addFriendSqlSixt($sql)
	{
		$this->addFriendSqlSixt = $sql;
	}

	//remove
	public function removeFriendSqlOneo($sql)
	{
		$this->removeFriendSqlOneo = $sql;
	}

	public function removeFriendSqlOnet($sql)
	{
		$this->removeFriendSqlOnet = $sql;
	}

	public function removeFriendSqlOne($sql)
	{
		$this->removeFriendSqlOne = $sql;
	}

	public function removeFriendSqlTwo($sql)
	{
		$this->removeFriendSqlTwo = $sql;
	}

	public function removeFriendSqlThree($sql)
	{
		$this->removeFriendSqlThree = $sql;
	}

	public function removeFriendSqlFour($field)
	{
		$this->removeFriendSqlFour = $field;
	}

	public function removeFriendSqlFourt($field)
	{
		$this->removeFriendSqlFourt = $field;
	}

		public function removeFriendSqlFive($sql)
	{
		$this->removeFriendSqlFive = $sql;
	}

	public function removeFriendSqlSix($sql)
	{
		$this->removeFriendSqlSix = $sql;
	}

	public function removeFriendSqlSixt($sql)
	{
		$this->removeFriendSqlSixt = $sql;
	}

	public function removeFriendSqlSeven($sql)
	{
		$this->removeFriendSqlSeven = $sql;
	}


	public function setUserNameRowField($user_name)
	{
		$this->userNameRowField = $user_name;
	}

	private function setAction()
	{
		if(!isset($_GET['action']))
		{
			$this->outputErrorMessage("Wrong parameter input");
		}
		else
		 return $_GET['action'];
	}

	private function setFriendGroup()
	{
			return $_GET['friendgroup'];
	}

	private function setXmlDoc()
	{
		$this->doc = new DOMDocument('1.0', 'utf-8');
		$this->FL = $this->doc->createElement('FL');
		$this->UD = $this->doc->createElement('UD');
	}

	private function responseSuccess()
	{
		$this->FL->setAttribute("r", 0);
		$this->doc->appendChild($this->FL);
				$this->outputXml();
				exit;
	}

	private function responseFailed()
	{
		$this->FL->setAttribute("r", 1);
		$this->doc->appendChild($this->FL);
		$this->outputXml();
				exit;
	}

	private function outputXml()
	{
		header("Content-Type: text/xml; charset=utf8");
		echo $this->doc->saveXml();
		exit;
	}

	private function outputErrorMessage($message)
	{
		exit($message);
	}
}
?>